package middlewares

import (
	"gitee.com/sweetsunnyflower/carpooling-server-go/app/models/driver"
	"github.com/gofiber/fiber/v2"
	"github.com/golang-jwt/jwt/v5"
	"github.com/spf13/cast"
)

func Driver() func(c *fiber.Ctx) error {
	return func(c *fiber.Ctx) error {
		user := c.Locals("user").(*jwt.Token)
		claims := user.Claims.(jwt.MapClaims)
		userId := cast.ToUint64(claims["user_id"].(string))
		// 查询司机信息
		if d := driver.GetByUserId(userId); d.ID > 0 {
			c.Locals("driver", d)
			return c.Next()
		}

		return c.Status(fiber.StatusUnprocessableEntity).JSON(map[string]interface{}{
			"message": "请先申请成为司机",
			"data":    map[string]interface{}{},
		})
	}
}
